<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                    	<input type="hidden" id="category" name="category" value="9FF2C95C723F4261A787465EBC650D7D" />
                        <div class="form-group">
                            <span class="col-sm-3 control-label">
	                            <label style="color:red">*</label>
	                            <label>标题：</label>
                            </span>
                            <div class="col-sm-8">
                                <input id="title" name="title" th:value="${info?.title}" class="form-control" type="text" maxlength="100">
                            </div>
                        </div>
                        <div class="form-group">
                         	<span class="col-sm-3 control-label">
                        		<label style="color:red">*</label>
								<label>所属分类：</label>
							</span>
							<div class="col-lg-8 col-sm-8 col-xs-8">
								<select  class="form-control chosen-select" id="classify" name="classify"></select>
								<input type="hidden" id="classifyValue" th:value="${info?.classify}"/>
							</div> 
						</div>
						<div class="form-group">
                            <span class="col-sm-3 control-label">
	                            <label style="color:red">*</label>
	                            <label>作者：</label>
                            </span>
                            <div class="col-sm-8">
                                <input id="zuozhe" name="zuozhe" th:value="${info?.zuozhe}" class="form-control" type="text" maxlength="100">
                            </div>
                        </div>
                        
                        <div class="form-group">
							<label class="col-sm-3 control-label ">是否推荐：</label>
							<div class="col-lg-8 col-sm-8 col-xs-8">
								<select  class="form-control chosen-select" id="top" name="top" >
									<option value ="2">不推荐</option>
  									<option value ="1">推荐</option>
								</select>
							</div>
						</div>
                        
                        <div class="form-group">
                            <span class="col-sm-3 control-label">
	                            <label style="color:red">*</label>
	                            <label>排序：</label>
                            </span>
                            <div class="col-sm-8">
                                <input id="sort" name="sort" th:value="${info?.sort}" class="form-control" type="number" >
                            </div>
                        </div>
                        
                        <div class="form-group">
                         	<span class="col-sm-3 control-label">
	                            <label style="color:red">*</label>
	                            <label>新闻视频：</label>
                            </span>
                         	<div class="col-sm-8">
		                        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="test1">
		                            <i class="fa fa-chain"></i>选择视频
		                        </button>  
		                        <div style="padding-top: 10px;"></div>
		                         <img width="120px" height="120px" class="layui-upload-img" id="demo2"  />  
								 <div style="padding-top: 10px;"></div>
								 <label>视频路径：</label><input  readonly type="text"  id="hasOne" name="hasOne" th:value="${info?.videopath}"  class="form-control" />
							 	 <input type="hidden" id="videopath" name="videopath" th:value="${info?.videopath}" />
							 	 <input type="hidden" id="videopicturepath" name="videopicturepath" th:value="${info?.videopicturepath}" />
							 </div>
						</div>
                        
                        
                        <div class="form-group mb0">
                        	<span class="col-sm-3 control-label">
	                            
	                            <label>内容：</label>
                            </span>
                            <div class="col-sm-8">
	                            <div class="ibox float-e-margins">
	                                <textarea id="content" name="content" ></textarea>
	                            </div>
                        	</div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
<!--                            <a class="btn btn-primary btn-sm " onclick="goBack()"><i class="fa fa-arrow-circle-left">返回</i></a> -->
		                        <button type="button" id="tijiao" class="btn btn-primary">提交</button>
	                        </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script type="text/javascript" src="/js/layui.js"></script>
<!-- <script type="text/javascript" src="/js/lucky/news/manage/add_picture.js"></script> -->
<script type="text/javascript" src="/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/ueditor/ueditor.all.js"></script>
<script type="text/javascript" src="/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="/js/plugins/layer/layer.min.js"></script>
<script type="text/javascript" th:inline="javascript">
    
    var files; 
    var i;
    layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1', //绑定元素
            auto:true, 
           // acceptMime: 'image/*',
            url:'/news/manage/uploadvideo',
            size: 1024 * 50,
            multiple:false,
            number:1,
            accept: 'video',
            before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            	 i=showLoad();
            },
            done: function(data){ 
            	if(data.success){
            		closeLoad(i);
        	    	$("#hasOne").val(data.fileURLs[0]); 
        	    	$("#demo2").attr('src',data.fileURLs[1]);
         	    	$("#videopath").val(data.fileURLs[0]);
    	    	  	$("#videopicturepath").val(data.fileURLs[1]); 
    	    	  	parent.layer.msg("上传成功!");
        	    }else{
        	    	closeLoad(i);
        	    	parent.layer.msg(data.message);
        	    }
            },
            error: function(){
            	parent.layer.msg("视频上传失败!");
            } 
        });
    });
    
    function showLoad() { 
        return layer.msg("上传中...", {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: 'auto', time:100000});
    }
     
    function closeLoad(index) {
    	console.log("===========");
        layer.close(index); 
    } 
     
   
var second = 0;
$("#tijiao").click(function(){
	if($("#signupForm").valid() && second == 0){
		second = second + 1;
		$("#tijiao").attr("style","display:none;");  
		
		var form = new FormData();   
		form.append("category",$("#category").val()); 
		form.append('title',$("#title").val());
		form.append('classify',$("#classify").val());
		form.append('zuozhe',$("#zuozhe").val());
		form.append('videopath',$("#videopath").val());
		form.append('videopicturepath',$("#videopicturepath").val());
		form.append('sort',$("#sort").val());
		form.append('content',ue.getContent());
		form.append('top',$('#top option:selected').val());
		 
		$.ajax({
			type : "POST",
			url : prefix+"/save",
			contentType:false,
			processData :false,
			data : form,
			async : false,
			 
			error : function(request) {
				 
				parent.layer.msg("通信出错!");
			},
			success : function(data) {
				 
				if (data.code == 0) {
					parent.layer.msg("操作成功");
					parent.location.reload();
					var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
					parent.layer.close(index);

				} else {
					parent.layer.msg(data.msg)
				}

			}
		}); 
	}
}); 
</script>
<script type="text/javascript" th:inline="javascript" >
	var ue = UE.getEditor('content',{
        initialFrameWidth : '100%',//宽度
        initialFrameHeight: 300//高度
    });
	// editor准备好之后才可以使用
    ue.addListener("ready", function(){
    	var content = [[${info?.content}]];
    	if(content != null && content != ""){
    		ue.setContent(content); 
    	} 
	}); 
	 
</script>
<script type="text/javascript">
var prefix = "/news/manage"
	$().ready(function(){ 
		selectedHtml("classify","F642C63D392B46E08DB92FA5250FCC6D", $("#classifyValue").val());
		validateRule(); 
	});

	$.validator.setDefaults({
		onsubmit:false,
		onfocusout: function(element) { $(element).valid(); }, 
	}); 

	function validateRule() { 
	    var icon = "<i class='fa fa-times-circle'></i> ";
	    $("#signupForm").validate({
	    	rules : {
	    		title : {
	    			required : true
	            },
	            classify : {
	            	required : true
	            }, 
	            zuozhe : {
	            	required : true
	            }, 
	            sort : {
	            	required : true,
	                digits : true,
	                maxlength : 12 
	            }, 
	            hasOne : {
	            	required : true
	            } 
	        },
	        messages : {
	        	title : {
	        		required : icon + "请输入标题"
	            },
	            classify : {
	            	required : icon + "选择分类"
	            }, 
	            zuozhe : {
	            	required : icon + "添加作者"
	            }, 
	            sort : {
	            	required : icon + "请输入正整数",
	            	digits : icon + "请输入正整数",
	            	maxlength : icon + "输入值过大"
	            },
	            hasOne:{
	            	required : icon + "请上传视频"
	            }
	        }
	    })
	}

	//字典
	var selectedHtml = function(selctId,gValue,hxId){
	    $("#"+selctId).empty();
	    $.ajax({
	        cache : true,
	        type : "GET",
	        url : "/common/sysDict/listByGroup",
	        data : {"gValue":gValue},
	        async : false,
	        error : function(request) {
	        	parent.layer.msg("通信出错!");
	        },
	        success : function(data) {
	            if (data.code == 0) {
	                var arr = data.data;
	                var option = "<option value=''>-请选择-</option>";
	                if(hxId == ""){
	                    option = "<option value='' selected='selected'>-请选择-</option>";
	                }else{
	                    option = "<option value='' >-请选择-</option>";
	                }
	                $("#"+selctId).append(option);
	                $.each(arr,function(index,obj){
	                    if(obj.value != null){
	                        var htmlRes = "";
	                        if(obj.id == hxId){
	                            htmlRes = "<option value='"+obj.id+"' selected='selected'>"+obj.name+"</option>";
	                        }else{
	                            htmlRes = "<option value='"+obj.id+"'>"+obj.name+"</option>";
	                        }
	                        $("#"+selctId).append(htmlRes);
	                    }
	                });
	            } else {
	            	parent.layer.msg(data.msg)
	            }
	        }
	    });
	};
</script>
</body>
</html>
